package com.cloudptt.wp.ui.address.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.cloudptt.wp.R;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.SysApplication;
import com.mcptt.db.model.AddrInfo;

import java.util.List;

public class AddrUserListAdapter extends BaseAdapter {

	private static final String THIS_FILE = "AddrUserListAdapter";
	private Context ctx = null;
	private List<AddrInfo> list = null;
	private AddrInfo mcIdInfo = null;
	private boolean isForwardUi = false;

	public AddrUserListAdapter(Context ctx, List<AddrInfo> list, AddrInfo mcIdInfo,boolean isForwardUi)
	{
		Log.i(THIS_FILE,"new list =  "+ list + " mcIdInfo = " + mcIdInfo + " isForwardUi = " + isForwardUi);
		this.ctx = ctx;
		this.list = list;
		this.mcIdInfo = mcIdInfo;
		this.isForwardUi = isForwardUi;
	}
	
	public void refresh(List<AddrInfo> list){
		Log.i(THIS_FILE,"updateData list =  "+ list);
		this.list = list;
		notifyDataSetChanged();
	}
	public void setMyLoginInfo(AddrInfo myInfo){
		Log.i(THIS_FILE,"setMyLoginInfo myInfo =  "+ myInfo);
		this.mcIdInfo = myInfo;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(list != null){
			return list.size();
		}else{
			return 0;
		}
		
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE,"getItem position =  "+ position);
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null)
		{
			LayoutInflater inflater = LayoutInflater.from(ctx);
			convertView = inflater.inflate(R.layout.item_addr, null);

			viewHolder = new ViewHolder();
			viewHolder.itemLy = (LinearLayout)convertView.findViewById(R.id.itemLy);
			viewHolder.userIcon       = (ImageView)convertView.findViewById(R.id.user_icon);
			viewHolder.userInfoLy =  (View)  convertView.findViewById(R.id.user_info);
			viewHolder.name    =  (TextView)  convertView.findViewById(R.id.user_name);

//			viewHolder.defaultTv = (TextView)convertView.findViewById(R.id.defaultTv);
//			viewHolder.mySelf = (TextView)convertView.findViewById(R.id.is_myself);

			viewHolder.voiceBtn = (ImageView) convertView.findViewById(R.id.call_button);
			viewHolder.videoBtn  =  (ImageView) convertView.findViewById(R.id.video_button);
//			viewHolder.divider = convertView.findViewById(R.id.child_divider);

			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}



		AddrInfo addrInfo = list.get(position);
		Log.i(THIS_FILE,"getView addrInfo =  "+ addrInfo);
		if(addrInfo.isSys()
				|| addrInfo.isDac())
		{
			viewHolder.userIcon.setImageResource(R.drawable.icon_main_user_face);
			viewHolder.voiceBtn.setVisibility(View.VISIBLE);
			viewHolder.videoBtn.setVisibility(View.VISIBLE);
			
		}else{
			viewHolder.userIcon.setImageResource(R.drawable.icon_main_user_face);
			viewHolder.voiceBtn.setVisibility(View.VISIBLE);
			viewHolder.videoBtn.setVisibility(View.VISIBLE);
		}
		if(isForwardUi)
		{
			viewHolder.itemLy.setBackground(null);
			viewHolder.voiceBtn.setVisibility(View.INVISIBLE);
			viewHolder.videoBtn.setVisibility(View.INVISIBLE);
		}
//		if(addrInfo.isGroupType())
//		{
//			if(addrInfo.isAffiliation()){
//				viewHolder.defaultTv.setVisibility(View.VISIBLE);
//			}else{
//				viewHolder.defaultTv.setVisibility(View.GONE);
//			}
//			Log.i(THIS_FILE,"getView  addrInfo = " + addrInfo);
//			viewHolder.mySelf.setVisibility(View.GONE);
//		}else{
//			if(addrInfo.isSameOne(mcIdInfo)){
//				Log.d(THIS_FILE, "getChildView in");
//				viewHolder.mySelf.setVisibility(View.VISIBLE);
//			}else{
//				viewHolder.mySelf.setVisibility(View.GONE);
//			}
//			viewHolder.defaultTv.setVisibility(View.GONE);
//		}
		
		String dscp = addrInfo.getDscp();
		viewHolder.name.setText(dscp);
//		viewHolder.userInfoLy.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View v) {
//				goToMsgActivity(addrInfo);
//			}
//		});
		viewHolder.videoBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				doClickCallBtnAction(addrInfo, true , CallAttr.CallType_Private);
			}
		});
		viewHolder.voiceBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				doClickCallBtnAction(addrInfo, false,CallAttr.CallType_Private);
			}
		});

		return convertView;
	}

	private void doClickCallBtnAction(AddrInfo userInfo, boolean useVideo, int videoType){
		Log.d(THIS_FILE, "doClickCallBtnAction userInfo = " + userInfo + " useVideo = " + useVideo + " mcIdInfo = " + mcIdInfo);
		if(userInfo.isSameOne(mcIdInfo)){
			CommDlg.show(ctx, ctx.getResources().getString(R.string.call_self));
			return;
		}

		//		if( useVideo && !isCameraUseed()){
		//			return;
		//		}
		int callType = CallAttr.CallType_Private;
		if(userInfo.isCamera())
		{
			String name = userInfo.getName();
			String rtspStr = userInfo.getAddrId();
//			CameraInfo info = userInfo.toCameraInfo();
			Log.d(THIS_FILE, "doClickCallBtnAction name = " + name + " rtspStr = " + rtspStr);
//			Intent intent = new Intent();
//			intent.setClass(ctx, CameraActivity.class);
//			intent.putExtra("name", name);
//			intent.putExtra("rtsp", rtspStr);
//			//		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			ctx.startActivity(intent);
		}
		else
		{
			if(useVideo)
			{
				callType = videoType;
			}
			SysApplication app = ((SysApplication) ctx.getApplicationContext());
			Product product = app.getProduct();
			product.getPCallEventAdapter().makePrivateCall(userInfo, callType,useVideo,false,"");
		}
		//		CallFunction.makePrivateCall(ctx,userInfo, callType,useVideo);
	}

	public void setDefaultGroupInfo(AddrInfo info)
	{
		Log.d(THIS_FILE, "setDefaultGroupInfo info = " + info);
		SysApplication app = ((SysApplication) ctx.getApplicationContext());
		Product product = app.getProduct();
		if(product != null)
		{
			product.setDefaultGroupInfo(info);
		}
	}

	private class ViewHolder {
		public LinearLayout itemLy;
		public ImageView userIcon;
		public View userInfoLy;
		public TextView  name;
//		public TextView defaultTv;
//		public TextView mySelf;
		public ImageView videoBtn;
		public ImageView voiceBtn;
//		public View divider;
	}
}
